using System;

namespace Nice157Advices.Advices
{
    /// <summary>
    /// 41.实现标准的事件模型
    /// </summary>
    public static class _41
    {
        /**
         * 事件模型设定的几个规范
         * - 委托类型的名称以 EventHandler 结束
         * - 委托原型返回值为 void
         * - 委托原型具有两个参数: sender(表示事件触发者) e(表示事件参数)
         * - 事件参数的名称以 EventArgs 结束
         */
        public static void Go ()
        {
            var _ = new FileUploader();
            _.FileUploaded += (object sender, FileUploadedEventArgs e) => System.Console.Write($"{e.FileProgress} ");

            _.Upload();
        }

        private class FileUploader {
            public event EventHandler<FileUploadedEventArgs> FileUploaded;
            public void Upload() {
                FileUploadedEventArgs e = new FileUploadedEventArgs() { FileProgress = 100 };
                while (e.FileProgress > 0)
                {
                    e.FileProgress--;
                    if (FileUploaded != null)
                    {
                        FileUploaded(this, e);
                    }
                }
            }
        }

        private class FileUploadedEventArgs : EventArgs
        {
            public int FileProgress { get; set; }
        }

    }
}